package cn.firecode.data_collect.controller;
import	java.awt.Desktop.Action;


import cn.firecode.data_collect.config.FileLocalCacheConfig;
import cn.firecode.data_collect.domain.entity.CollectDeviceT;
import cn.firecode.data_collect.service.CollectDeviceTService;
import cn.firecode.data_collect.utils.DataResponse;
import cn.firecode.data_collect.utils.FastDFSClientUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/device")
@Slf4j
public class CollectDeviceTController {

    @Autowired
    private CollectDeviceTService deviceTService;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public DataResponse add(@RequestParam("designFile") MultipartFile designFile, @RequestParam("ati") String atiInstallInfo,
                            String name, @RequestParam("thumFile") MultipartFile thumFile,String designFileMD5,String thumFileMD5){


        try {
            String localDesignFileMD5 = SecureUtil.md5(designFile.getInputStream());
            log.info("Receive Design File the File MD5 is:"+localDesignFileMD5);
            String localThumFileMD5 = SecureUtil.md5(thumFile.getInputStream());
            log.info("Receive Thum File the File MD5 is:"+localThumFileMD5);

            if(!localDesignFileMD5.toLowerCase().equals(designFileMD5.toLowerCase())){
                log.error("DesingFile MD5 本地校验未通过");
                return DataResponse.failed(7,"Desing File MD5校验未通过，请重新上传");
            }

            if(!localThumFileMD5.toLowerCase().equals(thumFileMD5.toLowerCase())){
                log.error("ThumFile MD5 本地校验未通过");
                return DataResponse.failed(7,"Thum File MD5校验未通过，请重新上传");
            }

            log.info("开始上传 designFile........");
            String desingFileFastDFSPath = fastDFSClientUtil.uploadFile(designFile);
            log.info("designFile上传成功 ："+desingFileFastDFSPath);


            log.info("开始上传 thumFile........");
            String thumFileFastDFSPath = fastDFSClientUtil.uploadFile(thumFile);
            log.info("thumFile上传成功 ："+desingFileFastDFSPath);

            CollectDeviceT deviceT = new CollectDeviceT();
            deviceT.setAtiInstallInfo(atiInstallInfo);
            deviceT.setCreateTime(LocalDate.now());
            deviceT.setDesignDrawing(desingFileFastDFSPath);
            deviceT.setThumbnailPath(thumFileFastDFSPath);
            deviceT.setDeviceName(name);
            deviceT.setIsDelete(0);
            boolean saveResult = deviceTService.save(deviceT);
            if(saveResult){
                log.info("新增头端装置成功:"+deviceT.toString());
                return DataResponse.succeed("新增成功");
            }else{
                return DataResponse.failed(1,"新增失败");
            }

        } catch (IOException e) {
            log.error("文件上床FastDFS服务器失败");
            return DataResponse.failed(7,"FastDFS 文件服务器上传失败");
        }


    }

    @RequestMapping(value = "/delete/{deviceId}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("deviceId") Integer deviceId){
        CollectDeviceT deviceT = deviceTService.getById(deviceId);
        if(deviceId==null){
            return DataResponse.failed(1,"id 错误，不存在");
        }
        deviceT.setIsDelete(1);
        boolean result = deviceTService.saveOrUpdate(deviceT);
        if(result){
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public DataResponse getList(){
        QueryWrapper<CollectDeviceT> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<CollectDeviceT> list = deviceTService.list(wrapper);
        return DataResponse.succeed(String.valueOf(list.size()),list);
    }
}
